package com.wmyg.antifake.service.impl;

import com.wmyg.antifake.entity.Product;
import com.wmyg.antifake.entity.WxRecord;
import com.wmyg.antifake.entity.WxUser;
import com.wmyg.antifake.vo.RecordVo;
import com.wmyg.antifake.repository.ProductRepository;
import com.wmyg.antifake.repository.WxRecordRepository;
import com.wmyg.antifake.repository.WxUserRepository;
import com.wmyg.antifake.service.WxRecordService;
import com.wmyg.antifake.vo.PictureVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 王卓逸
 * @program TODO
 * @description TODO
 * @create 2019-05-13
 */
@Service
public class WxRecordServiceImpl implements WxRecordService {

    @Autowired
    WxUserRepository wxUserRepository;

    @Autowired
    WxRecordRepository wxRecordRepository;

    @Autowired
    ProductRepository productRepository;

    @Override
    public List<RecordVo> selectWxRecordListByUser(String provisionalId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<RecordVo> recordVos = new ArrayList<>();
        WxUser user = wxUserRepository.findByProvisionalId(provisionalId);
        if (null != user) {
            List<WxRecord> wxRecords = user.getRecords();
            if (null != wxRecords) {
                for (WxRecord wxRecord :
                        wxRecords) {
                    RecordVo recordVo = new RecordVo();
                    recordVo.setRecordId(wxRecord.getRecordId());
                    recordVo.setRecordCode(wxRecord.getRecordCode());
                    recordVo.setAddress(wxRecord.getAddress());
                    recordVo.setQueryTime(sdf.format(wxRecord.getQueryTime()));
                    recordVo.setIsCollected(wxRecord.getCollected());
                    Product product = productRepository.findByProductId(wxRecord.getRecordCode().substring(5, 18));
                    if (StringUtils.isNotBlank(product.getProductLogo())) {
                        recordVo.setProductLogo(new PictureVo(product.getProductLogo()));
                    }
                    recordVo.setProductName(wxRecord.getProductName());
                    recordVos.add(recordVo);
                }
            }
        }
        return recordVos;
    }

    @Override
    public void delete(Integer recordId) {
        wxRecordRepository.deleteById(recordId);
    }

    @Override
    public Integer addCollection(Integer recordId) {
        WxRecord wxRecord = wxRecordRepository.findByRecordId(recordId);
        wxRecord.setCollected(1);
        wxRecordRepository.save(wxRecord);
        return wxRecordRepository.countByCollectedAndRecordCodeLike(1, "%" + wxRecord.getRecordCode().substring(5, 18) + "%");
    }

    @Override
    public void deleteCollection(Integer recordId) {
        WxRecord wxRecord = wxRecordRepository.findByRecordId(recordId);
        wxRecord.setCollected(0);
        wxRecordRepository.save(wxRecord);
    }

    @Override
    public List<RecordVo> getCollectionByUser(String provisionalId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<RecordVo> recordVos = new ArrayList<>();
        WxUser user = wxUserRepository.findByProvisionalId(provisionalId);
        if (null != user) {
            List<WxRecord> wxRecords = user.getRecords();
            if (null != wxRecords) {
                for (WxRecord wxRecord :
                        wxRecords) {
                    if (1 == wxRecord.getCollected()) {
                        RecordVo recordVo = new RecordVo();
                        recordVo.setRecordId(wxRecord.getRecordId());
                        recordVo.setRecordCode(wxRecord.getRecordCode());
                        recordVo.setAddress(wxRecord.getAddress());
                        recordVo.setQueryTime(sdf.format(wxRecord.getQueryTime()));
                        recordVo.setIsCollected(wxRecord.getCollected());
                        Product product = productRepository.findByProductId(wxRecord.getRecordCode().substring(5, 18));
                        if (StringUtils.isNotBlank(product.getProductLogo())) {
                            recordVo.setProductLogo(new PictureVo(product.getProductLogo()));
                        }
                        recordVo.setProductName(wxRecord.getProductName());
                        recordVos.add(recordVo);
                    }
                }
            }
        }
        return recordVos;
    }

}
